﻿using commonImportPlugin.Common;
using commonImportPlugin.Config;
using commonImportPlugin.Configs.MedicalFile;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace commonImportPlugin.FilesCore.MedicalFile
{
    /// <summary>
    /// 医保数据文件解析基础类
    /// </summary>
    public class MedicalFileBase : CommonUtil
    {   
        /// <summary>
        /// 获取每一个医保文件的数据
        /// </summary>
        /// <param name="allMedicalDatas">收集数据</param>
        /// <param name="medicalFilePath">医保文件</param>
        /// <param name="config">配置</param>
        /// <returns>true 成功，false失败</returns>
        public bool getFileMedicalDatas(List<MedicalData> allMedicalDatas, string medicalFilePath, MedicalFileConfig config)
        {
            string[] originMedicalLines = null;//所有初始医保数据         
            MedicalData medicalData = null;
            int[] subRules = null;

            if (!File.Exists(medicalFilePath))
                throw new Exception("医保文件不存在！");
            try
            {
                originMedicalLines = File.ReadAllLines(medicalFilePath, Encoding.GetEncoding(config.encoding));
                for (int i = config.dataIndex - 1; i < originMedicalLines.Length; i++)
                {
                    string[] medicalDatas = originMedicalLines[i].Split(config.dataSeparators, StringSplitOptions.None);
                    medicalData = new MedicalData();
                    //获取相应数据 
                    #region                
                    //银行卡号
                    if (config.subBankCardId!=null)
                    {
                        subRules = config.subBankCardId;
                        int column = subRules[0];
                        int subStart = subRules[1];
                        int subEnd = subRules[2];
                        medicalData.bankCardId = SubString(medicalDatas[column - 1].Trim(), subStart, subEnd);
                    }

                    //身份证号
                    if (config.subPersonId!=null)
                    {
                        subRules = config.subPersonId;
                        int column = subRules[0];
                        int subStart = subRules[1];
                        int subEnd = subRules[2];
                        medicalData.personId = SubString(medicalDatas[column - 1].Trim(), subStart, subEnd);
                    }       
       
                    //医保数据
                    if (config.subMedicalData != null)
                    {
                        string[] separators = config.addSeparators;
                        subRules = config.subMedicalData;
                        int column = subRules[0];
                        int subStart = subRules[1];
                        int subEnd = subRules[2];
                        string DataStr = SubString(medicalDatas[column - 1].Trim(), subStart, subEnd);
                        MedicalDataCheck(DataStr, config.dataCheckRegex);
                        medicalData.Data = string.Format("{0}{1}{2}", separators[0], DataStr, separators[1]);
                    }
                    #endregion

                    allMedicalDatas.Add(medicalData);
                }
            }
            catch (Exception e)
            {
                throw new Exception("提取医保数据报错："+e.Message) ;
            }       
            return true;
        }

        private bool MedicalDataCheck(string medicalData, string medicalDataRegexs)
        {
            Match match = null;
            bool isSucceed = true;
            string errorMessage = null;
            //检查磁条数据
            if (!string.IsNullOrEmpty(medicalDataRegexs))
            {
                if (!string.IsNullOrEmpty(medicalData))
                {
                    match = Regex.Match(medicalData.Trim(), medicalDataRegexs.Trim());
                    if (!match.Success)
                    {
                        isSucceed = false;
                        errorMessage = "提取的医保格式不正确";
                    }
                }
                else
                {
                    isSucceed = false;
                    errorMessage = "提取的医保数据为空";
                }
            }
            if (!isSucceed)
                throw new Exception(errorMessage);
            return isSucceed;
        }
    }
    public class MedicalData
    {
        /// <summary>
        /// 数据标识ID
        /// </summary>
        public int id { set; get; }
        /// <summary>
        /// 医保数据
        /// </summary>
        public string Data { set; get; }
        /// 银行卡号
        /// </summary>
        public string bankCardId { set; get; }
        /// <summary>
        /// 身份证号
        /// </summary>
        public string personId { set; get; }       
    }
}
